package com.ytjj.qmyx.admin.controller.supplyChain;


import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.model.Admin;
import com.ytjj.qmyx.admin.model.request.TaskListRequest;
import com.ytjj.qmyx.admin.service.CommonService;
import com.ytjj.qmyx.admin.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 供应链任务
 */
@RestController
@RequestMapping("/task")
@Slf4j
@RefreshScope
public class taskController {


    @Autowired
    private SupplyApiConfig supplyApiConfig;
    @Resource
    private MemberService memberService;
    @Resource
    private CommonService commonService;

    /**
     * 数据权限开关 true.开 false.关
     */
    @Value("${dataAuthor:false}")
    private Boolean dataAuthor;

    /**
     * 首页查询任务
     * @param waiting 1.待办任务 0.已处理任务
     * @return
     */
    @GetMapping("/queryWaitHandleTask")
    public String taskList(@RequestParam Integer waiting){
        Map<String,String> map = new HashMap<>();
        //当前登录人员
        Admin admin = memberService.getAdmin();
        if (!"admin@admin.com".equals(admin.getEmail())){
            map.put("adminName",admin.getCompellation());
        }
        map.put("waiting",String.valueOf(waiting));
        String s = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/task/queryWaitHandleTask", map, supplyApiConfig.getHeadsMap());
        return s;
    }
    /**
     * 任务列表
     */
    @PostMapping("/taskList")
    public String taskList(@RequestBody TaskListRequest request){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (dataAuthor){
            String adminName = commonService.getAdminName();
            request.setAdminName(StringUtils.isNotBlank(adminName) ? adminName : null);
        }
        if (null != request.getStartTime()){
            request.setStartTimeStr(sdf.format(request.getStartTime()));
        }
        if (null != request.getEndTime()){
            request.setEndTimeStr(sdf.format(request.getEndTime()));
        }
        String s = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/task/taskList", JSONObject.toJSONString(request), supplyApiConfig.getHeadsMap());
        return s;
    }

    /**
     * 任务详情
     */
    @GetMapping("/taskDetail")
    public String taskDetail(@RequestParam String taskId){
        //当前登录人员
        Admin admin = memberService.getAdmin();
        Map<String,String> map = new HashMap<>();
        map.put("taskId",taskId);
        map.put("adminName",admin.getCompellation());
        String s = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/task/taskDetail", map, supplyApiConfig.getHeadsMap());
        return s;
    }

    /**
     * 处理待办任务
     * @param taskId        任务id
     * @param result        处理结果  1-通过 2-拒绝
     * @param remark        拒绝原因，拒绝时的原因
     * @return
     */
    @GetMapping("/handleTask")
    public String handleTask(@RequestParam String taskId,@RequestParam Integer result,@RequestParam(required = false) String remark){
        //当前登录人员
        Admin admin = memberService.getAdmin();
        Map<String,String> map = new HashMap<>();
        map.put("taskId",taskId);
        map.put("result",result.toString());
        map.put("remark",remark);
        map.put("curUserName",admin.getCompellation());
        String s = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/task/handleTask", map, supplyApiConfig.getHeadsMap());
        return s;
    }
}
